package com.swyc.osschat.Test.LambdaTest.Controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.DoubleUnaryOperator;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import com.swyc.osschat.Test.LambdaTest.Bean.Student;

/**
 * @author zhaol@yunrong.cn
 * @version V2.1
 * @since 2.1.0 2019/8/12 21:47
 * UnaryOperator<T>
 * 提供单个参数，并且返回一个与输入参数类型一致的结果
 * BinaryOperator<T>
 * 提供两个参数，并且返回结果与输入参数类型一致的结果
 * DoubleBinaryOperator
 * 提供两个double参数并且返回double结果
 * DoubleUnaryOperator
 * 提供单个double参数并且返回double结果
 * IntBinaryOperator
 * 提供两个int参数并且返回int结果
 * IntUnaryOperator
 * 提供单个int参数并且返回int结果
 * LongBinaryOperator
 * 提供两个long参数并且返回long结果
 * LongUnaryOperator
 * 提供单个long参数并且返回long结果
 */
public class UnaryOperatorTest2 {
    private static final List<Student> students = Student.generateData();

    public static void main(String[] args) {
        UnaryOperator<Student> unaryOperator = a ->findSpouse(students,a.getSpouse());
        Student student1 = unaryOperator.apply(students.get(3));
        UnaryOperator<Student> unaryOperator1 = a -> Student.modifyStudentAge(a);
        Student student2 = unaryOperator.andThen(unaryOperator1).apply(students.get(3));
        Student student3 = unaryOperator.compose(unaryOperator1).apply(students.get(3));
        /** DoubleUnaryOperator提供单个double参数并且返回double结果 */
        DoubleUnaryOperator doubleUnaryOperator = doub -> doub + 2.5;
        double result3 = doubleUnaryOperator.applyAsDouble(10);
        DoubleUnaryOperator doubleUnaryOperator1 = doub -> doub * 3;
        double result1 = doubleUnaryOperator.andThen(doubleUnaryOperator1).applyAsDouble(10);
        double result2 = doubleUnaryOperator.compose(doubleUnaryOperator1).applyAsDouble(10);
        System.out.println(result1 + result2 + result3);
    }

    public static Student findSpouse(List<Student> list,Integer id){
        Map<Integer, Student> map = list.stream()
            .collect(Collectors.toMap(r -> r.getId(), r -> r, (v1, v2) -> v2));
        return Objects.nonNull(map.get(id))?map.get(id):students.get(6);
    }
}
